import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  data: {
    houseId: '',
    houseInfo: '',
    repairItemId: '',
    repairItemName: '',
    mobile: '',
    appointment: '',
    description: '',
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
  },
  rules: {
    houseId: [{
      required: true,
      message: '请选择报修房屋!'
    }],
    repairItemId: [{
      required: true,
      message: '请选择维修的项目!'
    }],
    mobile: [{
        required: true,
        message: '请填写手机号码!'
      },
      {
        pattern: /^1[3-8]\d{9}$/,
        message: '请填写正确的手机号码!'
      },
    ],
    appointment: [{
      required: true,
      message: '请选择预约日期!'
    }],
    description: [{
      required: true,
      message: '请填写问题描述!'
    }],
  },
  onLoad({
    id
  }) {
    this.getHouseList()
    this.getRepairItem()
    if (id) this.getRepairDetail(id)
  },
  // 报修详情接口
  async getRepairDetail(id) {
    if (!id) return wx.utils.toast('参数有误!')
    // 调用接口
    const {
      code,
      data: repairDetail
    } = await wx.http.get('/repair/' + id)
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({
      ...repairDetail
    })
  },
  // 获取审核通过房屋列表
  async getHouseList() {
    const res = await wx.http.get('/house')
    if (res.code !== 10000) return wx.utils.toast()
    this.setData({
      houseList: res.data
    })
  },
  // 选择房屋
  selectHouseInfo(e) {
    console.log(e);
    this.setData({
      houseId: e.detail.id,
      houseInfo: e.detail.name
    })
  },
  // 获取维修列表
  async getRepairItem() {
    const res = await wx.http.get('/repairItem')
    if (res.code !== 10000) return wx.utils.toast()
    this.setData({
      repairItem: res.data
    })
  },
  // 选择维修项目
  selectRepairIten(e) {
    console.log(e);
    this.setData({
      repairItemId: e.detail.id,
      repairItemInfo: e.detail.name
    })
  },
  // 选择时间
  selectDateFn(e) {
    console.log(e);
    this.setData({
      appointment: wx.utils.dataFormat(e.detail),
      dateLayerVisible: false
    });
  },
  // 上传图片
  uploadPictures(e) {
    const {
      file
    } = e.detail;
    wx.uploadFile({
      url: wx.http.baseURL + '/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: (res) => {
        console.log(res);
        const data = JSON.parse(res.data)
        if (data.code !== 10000) return wx.utils.toast('上传失败')
        // 上传成功的话需要修改一下上传列表数据
        const {
          attachment
        } = this.data
        attachment.push(data.data)
        this.setData({
          attachment
        })
      },
    });
  },
  // 删除图片
  deletePictures(e) {
    console.log(e);
    const {
      attachment
    } = this.data
    attachment.splice(e.detail.index, 1)
    this.setData({
      attachment
    })
  },
  // 提交表单
  async submitForm() {
    if (!this.validate()) return
    // 取到调用接口需要的数据
    const {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    } = this.data
    const {
      code
    } = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast()
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})